<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="发票类型" prop="lhinvoiceType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.lhinvoiceType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('lhinvoice_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用户" prop="userId"
                :rules="[
                 ]">
              <el-input v-model="inputForm.userId" placeholder="请填写用户"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用户类型" prop="issuerType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.issuerType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('issuer_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否开票" prop="isBiLing"
                :rules="[
                 ]">
                <el-select v-model="inputForm.isBiLing" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开票时间" prop="bilingTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.bilingTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="票据类型" prop="billType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.billType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('bill_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开票方" prop="incoiceMakerType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.incoiceMakerType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('incoicemaker_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开票类型" prop="makeInvoiceType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.makeInvoiceType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('billing_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="名称" prop="name"
                :rules="[
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="纳税人识别号" prop="taxpayerNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taxpayerNumber" placeholder="请填写纳税人识别号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="地址" prop="address"
                :rules="[
                 ]">
              <el-input v-model="inputForm.address" placeholder="请填写地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="电话" prop="phone"
                :rules="[
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写电话"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开户行" prop="bankOfDeposit"
                :rules="[
                 ]">
              <el-input v-model="inputForm.bankOfDeposit" placeholder="请填写开户行"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开户行账号" prop="accountNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.accountNumber" placeholder="请填写开户行账号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="发票内容" prop="invoicecontent"
                :rules="[
                 ]">
              <el-input v-model="inputForm.invoicecontent" placeholder="请填写发票内容"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单价" prop="unitPrice"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unitPrice" placeholder="请填写单价"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="金额" prop="price"
                :rules="[
                 ]">
              <el-input v-model="inputForm.price" placeholder="请填写金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="税率" prop="taxrate"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taxrate" placeholder="请填写税率"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="税额" prop="taxamount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taxamount" placeholder="请填写税额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="价税合计" prop="totalpriceandtax"
                :rules="[
                 ]">
              <el-input v-model="inputForm.totalpriceandtax" placeholder="请填写价税合计"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="收款人" prop="payee"
                :rules="[
                 ]">
              <el-input v-model="inputForm.payee" placeholder="请填写收款人"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="复核人" prop="review"
                :rules="[
                 ]">
              <el-input v-model="inputForm.review" placeholder="请填写复核人"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开票人" prop="drawer"
                :rules="[
                 ]">
              <el-input v-model="inputForm.drawer" placeholder="请填写开票人"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="票据" prop="bill"
                :rules="[
                 ]">
              <el-upload ref="bill"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/lhyg.lgy/lhinvoiceinformation/invoiceInformation`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.bill = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.bill = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="billArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        billArra: [],
        inputForm: {
          id: '',
          lhinvoiceType: '',
          userId: '',
          issuerType: '',
          isBiLing: '',
          bilingTime: '',
          bill: '',
          billType: '',
          incoiceMakerType: '',
          makeInvoiceType: '',
          name: '',
          taxpayerNumber: '',
          address: '',
          phone: '',
          bankOfDeposit: '',
          accountNumber: '',
          invoicecontent: '',
          unitPrice: '',
          price: '',
          taxrate: '',
          taxamount: '',
          totalpriceandtax: '',
          payee: '',
          review: '',
          drawer: '',
          remarks: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建灵活发票信息表`
        } else if (method === 'edit') {
          this.title = '修改灵活发票信息表'
        } else if (method === 'view') {
          this.title = '查看灵活发票信息表'
        }
        this.billArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/lhyg/lgy/lhinvoiceinformation/invoiceInformation/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.invoiceInformation)
              this.inputForm.bill.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.billArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/lhyg/lgy/lhinvoiceinformation/invoiceInformation/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>